package com.example.xm.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.mail.MailUtil;
import com.example.xm.Entity.Mail;
import com.example.xm.config.PageResult;
import com.example.xm.servive.MailServive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/U")
public class MailController {

    @Autowired
    MailServive mailServive;

    /**获取*/
    @PostMapping("/search")
    public List<Mail> Mail(@RequestBody Mail mail){
        System.out.println(mailServive.getMail(mail));
        return mailServive.getMail(mail);
    }

    /**分页*/
    @GetMapping("/pager")
    public PageResult selectPager(@RequestParam(name = "pageNum",required = false,defaultValue = "1") int pageNum,
                                  @RequestParam(name = "pageSize",required = false,defaultValue = "5") int pageSize,
                                  @RequestParam(required = false,defaultValue = "")String id,
                                  @RequestParam(required = false,defaultValue = "")String user,
                                  @RequestParam(required = false,defaultValue = "")String pass,
                                  @RequestParam(required = false,defaultValue = "")String qq,
                                  @RequestParam(required = false,defaultValue = "")String m_date
    ){
        //获取所有学生信息
        Mail m = new Mail();
        m.setId(id);
        m.setUser(user);
        m.setPass(pass);
        m.setQq(qq);
        if(!m_date.equals("") && !m_date.equals("null")){
            m.setM_date(DateUtil.parse(m_date,"yyyy-MM-dd"));
        }
        return mailServive.selectPager(pageNum,pageSize,m);
    }


    //发送邮箱
    @GetMapping("/n")
    public int num(HttpServletRequest req){
        //邮箱名
        String EnName = req.getParameter("qq");
        String mail = EnName.toString();
        //昵称
        String Name = req.getParameter("user");
        String name = Name.toString();
        //随机数
//        Random random=new Random();
//        int num=random.nextInt(100000);k
        String fs = req.getParameter("fs");
        String emo = MailUtil.send(mail+"@qq.com","smiiiiiile!",
                "<div style=\"background: rgb(33,36,41);width:775px;height:500px;margin: 0 auto;\">" +
                        "<div style=\"width:615px;margin:auto;\">" +
                        "<img src=\"/../resources/img/logo-mi2.png\" />" +
                        "<div style=\"text-align: left;color:rgb(191,191,191);\">"+"<h1>"+"您好,"+mail+"</h1>"+"<p>"+"您注册帐户所需的验证码为："+"<div style=\"background:rgb(23, 25, 28);height:112px;text-align: center;line-height: 112px;color:rgb(58,154,237);font-size: 32px;margin-bottom: 30px;font-weight: bolder;\">"+fs+"</div>"+"<div style=\"border-left: 5px solid rgb(58,154,237);height:84px;width:188px;text-align: center;line-height: 84px;\">祝您愉快，\n" +
                        "周边商城</div>"+"</div></div></div>"
                        +new Date(),true, FileUtil.file("D:\\个人\\Xm\\xm(Vue3)\\src\\assets\\img\\zfb.webp"));
        return 1;
    }

    /**
     * 添加*/
    @PostMapping("")
    public int getAdd(@RequestBody Mail mail){
        mail.setM_date(new Date());
        return mailServive.Add(mail);
    }

    /**删除*/
    @DeleteMapping("/{id}")
    public int del(@PathVariable String id){
        return mailServive.del(id);
    }


    /**根据qq来获取用户信息*/
    @GetMapping("/{qq}")
    public Mail getMailId(@PathVariable String qq){
        System.out.println(mailServive.getMailId(qq));
        return mailServive.getMailId(qq);
    }
    /**修改用户信息*/
    @PutMapping("")
    public int Update(@RequestBody Mail mail){
        System.out.println("修改了：" + mail);
        return mailServive.Update(mail);
    }
}
